package own.stu.jobgib.playown.thrift.client;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.*;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;
import own.stu.jobgib.playown.thrift.Person;
import own.stu.jobgib.playown.thrift.PersonService;
import own.stu.jobgib.playown.thrift.PersonServiceImpl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThriftClient {

    public static void main(String[] args) throws TException {

        /*ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 3; i++)
            executorService.submit(() -> {
                try {
                    // startClient_2();
                    // startClient_3();
                    // startClient();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });*/

        // startClient();
        // startClient_2();
        // startClient_3();
         startClient_4();
    }

    private static void startClient_4() throws TException {
        TTransport tTransport = new TFramedTransport(new TSocket(Args.host, Args.port), 600);
        TBinaryProtocol protocol = new TBinaryProtocol(tTransport);

        doBusiness(protocol, tTransport);
    }

    private static void startClient_3() throws TException {
        TSocket transport = new TSocket(Args.host, Args.port);

        TBinaryProtocol protocol = new TBinaryProtocol(transport);
        doBusiness(protocol, transport);
    }

    private static void startClient_2() throws TException {
        TSocket transport = new TSocket(Args.host, Args.port);

//        TBinaryProtocol protocol = new TBinaryProtocol(transport);
        TJSONProtocol protocol = new TJSONProtocol(transport);
        doBusiness(protocol, transport);
    }

    private static void startClient() throws TException {
        TTransport tTransport = new TFramedTransport(new TSocket(Args.host, Args.port), 600);
        TCompactProtocol protocol = new TCompactProtocol(tTransport);

        doBusiness(protocol, tTransport);
    }

    private static void doBusiness(TProtocol protocol, TTransport tTransport) throws TException {

        try {
            PersonService.Client client = new PersonService.Client(protocol);
            tTransport.open();

            Person person = client.getById(1);
            System.out.println(person);

            Person p = new Person().setAge(1).setId(2).setIsMarried(false).setName("解耦积分");
            client.save(p);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tTransport.close();
        }
    }

    private static class Args {
        static int port = 8899;
        static String host = "localhost";
    }
}
